package dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import bean.User;
import utils.DaoUtil;

public class UserDao {	
	public List<User> queryAll(){
		String sql = "select * from users";
		return DaoUtil.queryBeanList(null, User.class);
	}
	
	public User query4Login(User user){
		String sql = "select * from users where loginacct=? and userpswd=?";
		return DaoUtil.queryBean(sql, User.class, user.getLoginacct(),user.getUserpswd());
	}

	public List<User> pageQueryData(Map<String, Object> map){
		String queryText = (String)map.get("queryText");
		if(queryText!=null){
			String sql = "select * from users where loginacct like concat('%',?,'%') "
					+ "order by createtime desc limit ?,?";
			return DaoUtil.queryBeanList(sql, User.class
					, queryText,map.get("start"),map.get("size"));
		}
		else{
			String sql = "select * from users order by createtime desc limit ?,?";
			return DaoUtil.queryBeanList(sql, User.class
					, map.get("start"),map.get("size"));
		}		
	}

	public int pageQueryCount(Map<String, Object> map){
		String queryText = (String)map.get("queryText");
		if(queryText!=null){
			String sql = "select count(*) from users where loginacct like concat('%',?,'%')";
			return DaoUtil.count(sql, queryText);
		}
		else{
			String sql = "select count(*) from users";
			return DaoUtil.count(sql);
		}
	}

	public void insertUser(User user){
		String sql = "insert into users (loginacct, username, userpswd, email, createtime)"
				+ "values (?,?,?,?,?)";
		DaoUtil.insert(sql, user.getLoginacct(),user.getUsername(),
				user.getUserpswd(),user.getEmail(),user.getCreatetime());
	}

	
	public User queryById(Integer id){
		String sql = "select * from users where id = ?";
		return DaoUtil.queryBean(sql, User.class, id);
	}

	public void updateUser(User user){
		String sql = "update users  set loginacct =?, username =?, email =?"
				+" where id =?";
		DaoUtil.update(sql, user.getLoginacct(),user.getUsername(),
				user.getEmail(),user.getId());
	}

	public void deleteUserById(Integer id){
		String sql = "delete from users where id=?";
		DaoUtil.delete(sql, id);
	}

	public void deleteUsers(Map<String, Object> map){
		String[] userids = (String[])map.get("userids");
		if(userids!=null){
			for(String userid:userids){
				String sql ="delete from users where id=?";
				DaoUtil.delete(sql, userid);
			}
		}		
	}

	public void insertUserRoles(Map<String, Object> map){
		String userid = (String)map.get("userid");
		String[] roleids = (String[])map.get("roleids");
		if(roleids!=null){
			for(String roleid:roleids){
				String sql = "insert into user_role ( userid, roleid ) values(?,?)";
				DaoUtil.insert(sql, userid,roleid);
			}
		}		
	}

	public void deleteUserRoles(Map<String, Object> map){
		String userid = (String)map.get("userid");
		String[] roleids = (String[])map.get("roleids");
		if(roleids!=null){
			for(String roleid:roleids){
				String sql = "delete from user_role where userid =? and roleid=?";
				DaoUtil.delete(sql, userid,roleid);
			}
		}		
	}

	public List<Integer> queryRoleidsByUserid(Integer id){
		String sql = "select roleid from user_role where userid =?";
		List<Map<String,Object>> list =DaoUtil.queryMapList(sql, id);
		List<Integer> retList = new ArrayList<Integer>();
		if(list!=null){
			for(Map<String,Object> map:list){
				retList.add((Integer)map.get("roleid"));
			}
		}
		return retList;
	}

}
